//
//  BRInviteRewardView.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/2/15.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRInviteRewardView.h"

@interface BRInviteRewardView ()
{
    UIStackView *_stackView;
    QMUILabel *_titleL;
    QMUILabel *_subTitleL;
    QMUILabel *_numL;
    UIView *_numBGView;
}
@end

@implementation BRInviteRewardView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setUI];
    }
    return self;
}

- (void)showShadow {
    self.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
    self.layer.shadowOffset = CGSizeMake(0,10);
    self.layer.shadowRadius = 16;
    self.layer.shadowOpacity = 1;
    self.layer.masksToBounds = NO;
}

- (void)setType:(NSInteger)type {
    _type = type;
    CGFloat stackView_h = type < 3 ? 188 : 170;
    
    [_stackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(stackView_h);
    }];
    for (int i = 0; i<3; i++) {
        BRInviteTopImgView *itemV = (BRInviteTopImgView *)[_stackView viewWithTag:100 + i];
        itemV.type = type;
    }
}

- (void)setDto:(BRInviteRewardModel *)dto {
    _dto = dto;
    
    self.type = dto.rewardList.count;
    NSMutableArray *subTitles = [NSMutableArray array];
//    for (BRRewardModel *model in dto.rewardList) {
        
//        NSInteger itemType = model.type - 1;
//        if (model.type == 2) {
//            itemType = 2;
//        }
//        if (model.type == 3) {
//            itemType = 1;
//        }
//        [subTitles addObject:[self itemStrOf:model]];
//        if (itemType >= 0 && itemType < 3) {
//            [self itemOfType:itemType by:model];
//        }
//    }
    _titleL.text = [NSString stringWithFormat:Localized(@"Invite_inviteNumTitle"),dto.inviteeNum];
    _numL.text = [NSString stringWithFormat:@"%ldx",dto.inviteeNum];
    _subTitleL.text = [subTitles componentsJoinedByString:@" & "];
    _numBGView.backgroundColor = [self colorOfNum:dto.inviteeNum];
}

- (UIColor *)colorOfNum:(NSInteger)num {
    
    if (num >= 50) {
        //#FBE052
        return UIColorMakeWithRGBA(251, 224, 82, 1);
    }
    if (num >= 10) {
        //#BBCEF2
        return UIColorMakeWithRGBA(187, 206, 242, 1);
    }
    //#DEB18F
    return UIColorMakeWithRGBA(222, 177, 143, 1);
}

- (void)itemOfType:(NSInteger)type by:(BRRewardModel *)dto {
    BRInviteTopImgView *itemV = (BRInviteTopImgView *)[_stackView viewWithTag:100 + type];
    itemV.hidden = NO;
    itemV.imgStr = [NSString stringWithFormat:@"invite_reward%ld",type];
    
    itemV.titleStr = [self itemStrOf:dto];
}

- (NSString *)itemStrOf:(BRRewardModel *)dto {
    NSString *str = @"";
    if ([dto.type isEqualToString:@"1"]) {
        str = [NSString stringWithFormat:@"%@: %ld",Localized(@"Rewards_points"),dto.point];
    }else if ([dto.type isEqualToString:@"3"]) {
        str = [NSString stringWithFormat:@"%@: %@ %.2f",Localized(@"Invite_rewardCredits"),BRUserManager.share.currency.uppercaseString,dto.amount.floatValue];
    }else if ([dto.type isEqualToString:@"2"]) {
        str = dto.couponTitle;
    }
    return str;
}

#pragma mark - SET UI --
- (void)setUI {
    
    self.backgroundColor  = UIColorClear;
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor  = UIColor.qd_backgroundColor2;
    bgView.layer.cornerRadius = 33.f;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    UIView *topV = [[UIView alloc] init];
    topV.backgroundColor = UIColorClear;
    [bgView addSubview:topV];
    
    QMUILabel *titleL = [[QMUILabel alloc] init];
    titleL.text = Localized(@"Invite_inputCodeText");
    titleL.textColor = UIColor.qd_titleTextColor;
    titleL.textAlignment = NSTextAlignmentLeft;
    titleL.font = [UIFont br_fontM:17];
    titleL.numberOfLines = 1;
    [topV addSubview:titleL];
    
    QMUILabel *creditsL = [[QMUILabel alloc] init];
    creditsL.text = Localized(@"Invite_inputCodeText");
    creditsL.textColor = UIColor.qd_titleTextColor;
    creditsL.textAlignment = NSTextAlignmentLeft;
    creditsL.font = [UIFont br_fontB:17];
    creditsL.numberOfLines = 1;
    [topV addSubview:creditsL];
    
    UIView *numBGView = [[UIView alloc] init];
    [topV addSubview:numBGView];
    numBGView.layer.cornerRadius = 20.f;
    numBGView.layer.masksToBounds = YES;
    numBGView.backgroundColor = UIColorYellow;
    numBGView.layer.borderWidth = 3.5;
    numBGView.layer.borderColor  = UIColorMakeWithRGBA(0, 0, 0, 0.05).CGColor;
    
    QMUILabel *countL = [[QMUILabel alloc] init];
    countL.text = @"30x";
    countL.textColor = UIColorWhite;
    countL.textAlignment = NSTextAlignmentCenter;
    countL.font = [UIFont br_fontB:14];
    countL.numberOfLines = 1;
    [numBGView addSubview:countL];
    
    _titleL = titleL;
    _subTitleL = creditsL;
    _numL = countL;
    _numBGView = numBGView;
    
    [numBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.top.equalTo(topV).offset(20);
        make.right.offset(-28);
    }];
    [countL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(numBGView);
    }];
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.offset(15);
        make.right.offset(-96);
    }];
    [creditsL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(titleL.mas_bottom).offset(4);
        make.right.offset(-96);
        make.bottom.offset(-15);
    }];
    
    [topV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(bgView);
        make.height.mas_greaterThanOrEqualTo(80);
    }];
    
    UIView *line = [BRToolsView getLineView];
//    line.backgroundColor = UIColor.qd_lineColor2;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(topV);
        make.height.mas_equalTo(1);
    }];
    
    UIStackView *stackView = [[UIStackView alloc] init];
    stackView.axis = UILayoutConstraintAxisHorizontal;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.spacing = 0;
    [bgView addSubview:stackView];//104(85) + 64
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topV.mas_bottom).offset(0);
        make.right.left.offset(0);
        make.height.mas_equalTo(170);
        make.bottom.equalTo(bgView.mas_bottom).offset(0);
    }];
    _stackView = stackView;
    NSArray *titles = @[Localized(@"Rewards_inviteAFriend"),Localized(@"Rewards_viewPointHistory"),Localized(@"Rewards_earnPoints")];
    for (int i = 0; i < titles.count; i++) {
        
        BRInviteTopImgView *itemV = [[BRInviteTopImgView alloc] initWithImg:[NSString stringWithFormat:@"invite_reward%d",i] title:titles[i]];
        [stackView addArrangedSubview:itemV];
        itemV.userInteractionEnabled = YES;
        itemV.tag = 100 + i;
        itemV.type = 1;
        itemV.hidden = YES;
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@interface BRInviteTopImgView ()
{
    UIImageView *_img;
    QMUILabel *_titeL;
}

@end

@implementation BRInviteTopImgView

- (instancetype)initWithImg:(NSString *)imgStr title:(NSString *)title
{
    self = [super init];
    if (self) {
        
        [self setUI:imgStr title:title];
    }
    return self;
}

- (void)setType:(NSInteger)type {
    _type = type;
    CGFloat width = 80.;
    if (type < 3) {
        
        width = 104.;
        
    }
    
    [_img mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(width);
    }];
    
    
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    _titeL.text = titleStr;
}

- (void)setImgStr:(NSString *)imgStr {
    _imgStr = imgStr;
    _img.image = UIImageMake(imgStr);
}

- (void)setUI:(NSString *)imgStr title:(NSString *)title {
    
    self.backgroundColor = UIColorClear;
    UIImageView *img = [[UIImageView alloc] init];
    img.image = UIImageMake(imgStr);
    [self addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(80);
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(14);
    }];
    
    QMUILabel *pointL = [BRToolsView br_label:title textColor:kColorTitle font:[UIFont fontS:14] textAlignment:NSTextAlignmentCenter];
    pointL.numberOfLines = 0;
    [self addSubview:pointL];
    [pointL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-14);
        make.top.equalTo(img.mas_bottom).offset(14);
        make.left.offset(14);
        make.right.offset(-14);
    }];
    _img = img;
    _titeL = pointL;
}

@end
